﻿using System;
using System.Runtime.InteropServices;
using System.Text;

namespace Reactor.Utilities
{
    /// <summary>  
    /// It's just like a System.Collections.Generic.KeyValuePair,   
    /// but the XmlSerializer will serialize the   
    /// Key and Value properties!  
    /// </summary>  
    [Serializable, StructLayout( LayoutKind.Sequential )]  
    public struct SerializableKeyValuePair<TKey, TValue>  
    {  
        private TKey _key;  
        private TValue _value;  
  
        /// <summary>
        /// Initializes a new instance of the <see cref="SerializableKeyValuePair&lt;TKey, TValue&gt;"/> struct.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="value">The value.</param>
        public SerializableKeyValuePair(TKey key, TValue value)  
        {  
            _key = key;  
            _value = value;  
        }  
  
        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()  
        {  
            var builder = new StringBuilder();  
            builder.Append( '[' );
            if (Key != null)
                builder.Append(Key.ToString());

            builder.Append( ", " );
            if (Value != null)
                builder.Append(Value.ToString());

            builder.Append( ']' );  
            return builder.ToString();  
        }  
  
        /// <summary>  
        /// Gets or sets the Value in the Key/Value Pair  
        /// </summary>  
        public TValue Value  
        {  
            get { return _value; }
            set { _value = value; }
        }  
  
        /// <summary>  
        /// Gets the Key in the Key/Value pair  
        /// </summary>  
        public TKey Key  
        {  
            get { return _key; }
            set { _key = value; }
        }  
    }
}
